package com.cictec.middleware.gps.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.cictec.middleware.CloudConstants;
import com.cictec.middleware.core.entity.Bus;
import com.cictec.middleware.gps.service.feign.MiddlewareDcFeignClient;

/**
 * 车辆信息服务
 * 
 * @author ql
 *
 */
@Service
public class BusService {

	private static Logger logger = LoggerFactory.getLogger(BusService.class);

	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	@Autowired
	private MiddlewareDcFeignClient middlewareDcFeignClient;


	/**
	 * 【数据库】获取所有车辆信息
	 * 
	 * @return
	 */
	public List<Bus> selectListAllBus() {
		HashOperations<String, String, String> ops = stringRedisTemplate.opsForHash();
		
		List<Bus> list = new ArrayList<Bus>();
		List<String> json = ops.values(CloudConstants.REDIS_CLOUD_BUS);
		for (String string : json) {
			list.add(JSON.parseObject(string, Bus.class));
		}
		return list;
	}

	/**
	 * 【数据库】查询当前所有在线车辆
	 * 
	 * @return
	 */
	public List<Bus> selectListAllOnlineBus() {
		List<Bus> list = selectListAllBus();
		return list;
	}

	/**
	 * 【数据库】根据线路查询车辆(查询所属线路下所有车辆)
	 * 
	 * @param lineUuid
	 * @return
	 */
	public List<Bus> selectBusByLineUuid(String lineUuid) {
		List<Bus> list = selectListAllBus();
		List<Bus> rtList = new ArrayList<Bus>();
		for (Bus bus : list) {
			if (bus.getBusLineUuid().equals(lineUuid)) {
				rtList.add(bus);
			}
		}
		return rtList;
	}

	/**
	 * 【数据库】查询车辆信息
	 * 
	 * @param busUuid
	 * @return
	 */
	public Bus selectBusByUuid(String busUuid) {
		List<Bus> list = selectListAllBus();
		Bus rtBus = null;
		for (Bus bus : list) {
			if (bus.getBusUuid().equals(busUuid)) {
				rtBus = bus;
				break;
			}
		}

		return rtBus;
	}

	/**
	 * 【数据库】根据设备编号查询车辆，包含devCode
	 * 
	 * @param devCode
	 * @return
	 */
	public Bus selectBusByDevCode(String devCode) {
		List<Bus> list = selectListAllBus();
		Bus rtBus = null;
		for (Bus bus : list) {
			if (bus.getDevCode().equals(devCode)) {
				rtBus = bus;
				break;
			}
		}
		return rtBus;

	}

	/**
	 * 【数据库】根据车牌号查询车辆，包含devCode
	 * 
	 * @param busNumber
	 * @return
	 */
	public Bus selectBusByBusNumber(String busNumber) {
		List<Bus> list = selectListAllBus();
		Bus rtBus = null;
		for (Bus bus : list) {
			if (bus.getBusPlateNumber().equals(busNumber)) {
				rtBus = bus;
				break;
			}
		}
		return rtBus;
	}

	/**
	 * 更新当前车辆司机
	 * 
	 * @param busUuid
	 * @param drvUuid
	 * @param loginTime
	 */
	public void updateBusLoginDriver(String busUuid, String drvUuid, Date loginTime) {
		middlewareDcFeignClient.updateBusLoginDriver(busUuid, drvUuid, loginTime);
	}

	/**
	 * 更新车辆的强制非运营状态
	 * 
	 * @param bus
	 */
	public void updateForceNonoperStatus(Bus bus) {
		middlewareDcFeignClient.updateForceNonoperStatus(bus);

		logger.info("【busService】更新车辆表 {}, 设置 bus_force_non_oper :{} ", bus.getBusUuid(), bus.getBusForceNonOper());
	}

	/**
	 * 更新车辆所在线路
	 * 
	 * @param bus
	 */
	public void updateBusWorkingLine(Bus bus) {
		middlewareDcFeignClient.updateBusWorkingLine(bus);

		logger.info("【busService】更新车辆表 {}, 设置 bus_working_line_uuid:{} ", bus.getBusUuid(),
				bus.getBusWorkingLineUuid());
	}

}
